/*
 * TOSEInstaller.cpp
 *
 * This file is a part of TermOSEmulator.
 *
 * Written by Peter Winchester <wincsoft_pw@outlook.com>
 * Copyright (C) 2021 Peter Winchester.
 *
 * TermOSEmulator is free software, follows the GNU General
 * Public License.
 *
 * NOTE: TermOSEmulator requires Windows API, so it can only
 * run on the Windows.
 *
 */

#include "TermOSEmulator.h"

string command;
vector<string> cmdArgs;

int startUp();
int checkPath(char *);
int mount();
int install();

int main(int argc, char const *argv[])
{
    /* 加载开始界面 */
    if (!startUp())
        return 0;

    /* 挂载根目录 */
    if (!mount())
    {
        printf("\nError! Please check your root path!\n");
        printf("If you have installed TermOSEmulator in your root path before,\n");
        printf("please remove it first.\n");
        cmdArgs.clear();
        pause(cmdArgs);
        return 0;
    }

    /* 安装 */
    if (!install())
    {
        cmdArgs.clear();
        pause(cmdArgs);
        return 0;
    }

    /* 安装完成 */
    printf("Now you have installed TermOSEmulator.\n");
    printf("You can go to /bin/ and run TermOSEmulator.exe to start TermOSEmulator.\n");
    cmdArgs.clear();
    pause(cmdArgs);
    return 0;
}

//显示开始界面并搞定根目录
int startUp()
{
    color(7);
    printf("Welcome to TermOSEmulator Installer.\n");
    printf("Please type a path to mount the root directory /(The last character is '/' or '\\'):\n");

    /* 在这个循环中获取根目录 */
    while (true)
    {
        gets(systemRootPath);
        int len = strlen(systemRootPath);
        if (!len) return 0;
        if (systemRootPath[len - 1] != '/' & systemRootPath[len - 1] != '\\') continue;
        if (checkPath(systemRootPath)) break;
        printf("Cannot find the path, please type again or type ENTER to exit:\n");
    }
    return 1;
}

//检查目录是否合法
int checkPath(char *path)
{
    static char address[MAX_STRING_LEN];
    for (int i = 0; i < strlen(path); i++) address[i] = path[i];
    if (access(address, 0) != -1) return 1; //目录合法
    return 0;
}

//挂载根目录
int mount()
{
    printf("Mounting...");

    /* 创建根目录 */
    strcat(systemRootPath, "TermOSEmulator/");
    if (!CreateDirectoryA((LPCSTR)systemRootPath, NULL)) return 0;

    static char dir[MAX_STRING_LEN]; //将要创建的目录

    /* 创建 /bin 目录 */
    strcat(dir, systemRootPath);
    strcat(dir, "bin/");
    if (!CreateDirectoryA((LPCSTR)dir, NULL)) return 0;

    /* 创建 /bin/help 目录 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/");
    if (!CreateDirectoryA((LPCSTR)dir, NULL)) return 0;

    /* 创建 /home 目录 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "home/");
    if (!CreateDirectoryA((LPCSTR)dir, NULL)) return 0;

    /* 创建 /dat 目录 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "dat/");
    if (!CreateDirectoryA((LPCSTR)dir, NULL)) return 0;

    /* 创建 /etc 目录 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "etc/");
    if (!CreateDirectoryA((LPCSTR)dir, NULL)) return 0;

    /* 完成挂载 */
    printf("done!\n");
    return 1;
}

//安装 TermOSemulator
int install()
{
    printf("Installing...");
    FILE *fin, *fout;

    /* 复制文件 TermOSEmulator.exe. */
    fin = fopen("TermOSEmulator.exe", "rb");
    if (fin == NULL)
    {
        printf("\nError: cannot find TermOSEmulator.exe.\n");
        return 0;
    }
    char dir[MAX_STRING_LEN];
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/TermOSEmulator.exe");
    fout = fopen(dir, "wb");
    char cGet;
    while (fscanf(fin, "%c", &cGet) == 1)
        fprintf(fout, "%c", cGet);
    fclose(fin);
    fclose(fout);

    /* 保存根目录 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "dat/rootpath.txt");
    fout = fopen(dir, "w");
    for (int i = 0; i < strlen(systemRootPath); i++)
    {
        fprintf(fout, "%c", systemRootPath[i]);
    }
    fprintf(fout, "\r");
    fclose(fout);

    /* 初始化资源管理器数据 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "dat/explorerdat.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "md bin\r");
    fprintf(fout, "cd bin\r");
    fprintf(fout, "md help\r");
    fprintf(fout, "cd help\r");
    fprintf(fout, "mf help txt\r");
    fprintf(fout, "mf pause txt\r");
    fprintf(fout, "mf exit txt\r");
    fprintf(fout, "mf ls txt\r");
    fprintf(fout, "mf cd txt\r");
    fprintf(fout, "mf mkdir txt\r");
    fprintf(fout, "mf rmdir txt\r");
    fprintf(fout, "mf view txt\r");
    fprintf(fout, "mf apt txt\r");
    fprintf(fout, "mf rmf txt\r");
    fprintf(fout, "cd ..\r");
    fprintf(fout, "mf TermOSEmulator exe\r");
    fprintf(fout, "cd ..\r");
    fprintf(fout, "md dat\r");
    fprintf(fout, "cd dat\r");
    fprintf(fout, "mf sysappdat txt\r");
    fprintf(fout, "mf rootpath txt\r");
    fprintf(fout, "mf explorerdat txt\r");
    fprintf(fout, "cd ..\r");
    fprintf(fout, "md etc\r");
    fprintf(fout, "md home\r");
    fclose(fout);

    /* 初始化应用程序数据 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "dat/sysappdat.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "0\r");
    fclose(fout);

    /* 创建帮助文档 */
    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/help.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rType help <command> for more information about a command.\r\r");
    fprintf(fout, "Command Function\r");
    fprintf(fout, "------- --------\r");
    fprintf(fout, "pause   Pause.\r");
    fprintf(fout, "exit    Exit.\r");
    fprintf(fout, "ls      Display folders and files in the current directory.\r");
    fprintf(fout, "cd      Change directory.\r");
    fprintf(fout, "mkdir   Create a new directory in the current directory.\r");
    fprintf(fout, "rmdir   Remove a folder in the current directory.\r");
    fprintf(fout, "view    Display the contents of a text file.\r");
    fprintf(fout, "help    Display this help document.\r");
    fprintf(fout, "apt     Install or remove an application.\r");
    fprintf(fout, "rmf     Remove a file in the current directory.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/pause.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: pause\r");
    fprintf(fout, "Usage: pause\r");
    fprintf(fout, "Function: Pause and then press any key to continue.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/exit.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: exit\r");
    fprintf(fout, "Usage: exit\r");
    fprintf(fout, "Function: Exit TemrOSEmulator.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/ls.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: ls\r");
    fprintf(fout, "Usage: ls\r");
    fprintf(fout, "Function: List all folders and files in current directory.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/cd.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: cd\r");
    fprintf(fout, "Usage: cd <directory name>\r");
    fprintf(fout, "Function: Change the location to another directory, including \r");
    fprintf(fout, "          child directories and father directory.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/mkdir.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: mkdir\r");
    fprintf(fout, "Usage: mkdir <directory name>\r");
    fprintf(fout, "Function: Create a new child directory.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/rmdir.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: rmdir\r");
    fprintf(fout, "Usage: rmdir <directory name>\r");
    fprintf(fout, "Function: Remove a directory as well as its child directories and files.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/view.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: view\r");
    fprintf(fout, "Usage: view <file name>\r");
    fprintf(fout, "Function: Show the contents of a text file.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/apt.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: apt\r");
    fprintf(fout, "Usage: apt <mode>\r\r");
    fprintf(fout, "Mode       Function\r");
    fprintf(fout, "------     --------\r");
    fprintf(fout, "get        Install an application from Windows.\r");
    fprintf(fout, "remove     Remove an application.\r");
    fprintf(fout, "list       List installed applications.\r");
	fprintf(fout, "autoremove Remove apps which are no longer needed.\r\r");
    fprintf(fout, "Note: If you want to run an app, just type its name.\r\r");
    fclose(fout);

    memset(dir, 0, sizeof(dir));
    strcat(dir, systemRootPath);
    strcat(dir, "bin/help/rmf.txt");
    fout = fopen(dir, "w");
    fprintf(fout, "\rCommand Name: rmf\r");
    fprintf(fout, "Usage: rmf <file name>\r");
    fprintf(fout, "Function: Remove a file in the current directory.\r\r");
    fclose(fout);

    printf("done!\n"); //完成安装
    return 1;
}
